package qing.web;

import static org.junit.Assert.*;

import java.sql.SQLException;
import java.util.Date;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import qing.common.des;
import qing.dao.MoneyLogDao;
import qing.dao.PersonDao;
import qing.entity.Node;
import qing.entity.Person;
import qing.entity.moneyLog;
import qing.service.NodeService;
import qing.service.moneyLogService;
import qing.service.personService;

@SuppressWarnings("unchecked")
public class testSSH {

	
	private personService iPerson;
	
	private moneyLogService imoneyLog;
	
	//dao 层不可调用，所有方法均可以通过service层调用
	
	
	private ApplicationContext ctx = null;
	
	{
	ctx=new ClassPathXmlApplicationContext("applicationContext.xml");	
	iPerson=ctx.getBean(personService.class);
	imoneyLog=ctx.getBean(moneyLogService.class);

	}


	
	@Test
	public void testDataSource() throws SQLException {
		javax.sql.DataSource dataSource = ctx.getBean(javax.sql.DataSource.class);
		System.out.println(dataSource.getConnection());
		
		
	}
	
	@Test
	public void testmd5() 
	{
		String str="123456";
		System.out.println("md5:"+des.md5(str));
		String aes=des.AESEncode(str);
		System.out.println("AES加密："+aes);
		System.out.println(des.AESEncode(str));
		System.out.println("AES解密："+des.AESDncode(aes));
	}
	
	
	
	@Test
	public void testaddperson(){
		Person person=new Person("whx", "123456",102f);		
		System.out.println("starting save……");
		iPerson.create(person);
		System.out.println("person:"+person);
	}
	
	@Test
	public void  testwithsave() {
System.out.println(imoneyLog.savemoney(1, 10f));
		
	}
	@Test
	public void  testwithdraw() {
System.out.println(imoneyLog.drawmoney(1, 900f));
		
	}
	
	//测试事物是否只控制在被扫描的包里面，包外为非事物控制区域(证实为非事物控制),即使使用@Transactional约束，也为非事物
	@Test	
	 public void testtransatoutpackage() {
		 Person person=iPerson.find(Person.class, 1);
		 Float money=10020f;		   
			person.setAccount(person.getAccount()-money);
			imoneyLog.create(new moneyLog("测试取钱", -money, "junit test **"+new Date(), person));
			if (person.getAccount()<money) {
				throw new RuntimeException("账户余额不足，资产" + person.getAccount());
			}
			iPerson.update(person);
			System.out.println("over**********："+person.getAccount());
	 }
	
	
	//查询moneylog，并试图查询人员，测试懒加载(可以使用,需要开启<prop key="hibernate.enable_lazy_load_no_trans">true</prop>)
	//测试环境不能实现懒加载，正式环境可以实现
	@Test
	public void testfindmoneylog() {
		moneyLog log=imoneyLog.find(moneyLog.class, 1);
		System.out.println(log.getTitle());
		/*try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	   System.out.println("person:"+log.getPerson());	
	}
	

/*	//无法测试懒加载方法，由于没法限制延长session关闭时间
	@Test
	public void  testlazyload() {
Category category=iCategory.find(Category.class, 1);
System.out.println(category.getName());
System.out.println("start lazy:");
System.out.println(category.getBoards());
	}*/
	
	
	
	@Test
	public void testgetperson() {
		Person person=iPerson.getPersonByAccount("whx");
		System.out.println(person==null?"person is null": person.toString());
	}
	@Test
	public void getpersonbyId() {
		Person person=iPerson.find(Person.class, 1);
		System.out.println(person);
	person.setName("test_ssh update");
	iPerson.update(person);
	System.out.println("update over");
	
	}
	
	
	

}
